package com.qjn.study.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qjn.study.domain.UserEntity;
import com.qjn.study.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by sun on 2017/10/29.
 */


@RestController
@RequestMapping("/")
public class HelloController {

    @Autowired
    private UserMapper userMapper;

    /**
     * Hello world 接口
     *
     * @param name
     * @return
     */
    @RequestMapping("/hello")
    public String sayHello(String name) {
        if (StringUtils.isEmpty(name)) {
            return "Hello world";
        } else {
            return "Hello " + name;
        }
    }


    /**
     * 按照id查询用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getuser")
    public UserEntity getUser(Integer id) {
        UserEntity user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    /**
     * 查询用户信息列表
     *
     * @return
     */
    @RequestMapping("/getuserlist")
    public List<UserEntity> getUserList() {
        Condition condition = new Condition(UserEntity.class);
        List<UserEntity> userList = userMapper.selectByCondition(condition);
        return userList;
    }

    /**
     * 分页查询用户信息
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/getuserpage")
    public PageInfo getUserPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<UserEntity> userList = userMapper.selectAll();
        PageInfo pageInfo = new PageInfo(userList);
        return pageInfo;
    }
}
